<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('处理申请列表')" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>申请人：</label>
                            <input type="text" name="sysUser.userName"/>
                        </li>
                        <li>
                            <label>房屋名称：</label>
                            <input type="text" name="houHouse.name"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="admin:apply:export">
                <i class="fa fa-download"></i> 导出
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer" />
<script th:inline="javascript">
    var editFlag = [[${@permission.hasPermi('admin:apply:edit')}]];
    var removeFlag = [[${@permission.hasPermi('admin:apply:remove')}]];
    var prefix = ctx + "admin/apply";

    $(function() {
        var options = {
            url: prefix + "/listWithDetail/2",
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            modalName: "租房申请",
            columns: [{
                checkbox: true
            },
                {
                    field: 'applyId',
                    title: '申请编号'
                },
                {
                    field: 'houHouse.name',
                    title: '房屋名称'
                },
                {
                    field: 'sysUser.userName',
                    title: '申请人'
                },
                {
                    field: 'time',
                    title: '申请开始时间'
                },
                {
                    field: 'month',
                    title: '申请月数',
                    align: "center"
                },
                {
                    field: 'status',
                    title: '审批状态',
                    formatter: function (value, row, index) {
                        if (row.status == 1) {
                            return '<strong>等待审批</strong>';
                        } else if (row.status == 2) {
                            return '<strong>申请成功</strong>';
                        } else if (row.status == 3) {
                            return '<strong>申请失败</strong>';
                        }
                    },
                    cellStyle: function (value, row, index) {
                        if (row.status == 1) {
                            return {
                                css: {
                                    'color': '#565956'
                                }
                            };
                        } else if (row.status == 2) {
                            return {
                                css: {
                                    'color': '#088604'
                                }
                            };
                        } else if (row.status == 3) {
                            return {
                                css: {
                                    'color': '#ce0606'
                                }
                            };
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        if(row.status != 1){
                            actions.push('<a class="btn btn-warning btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="approval('+row.applyId+','+1+')"><i class="fa fa-mail-reply"></i></a> ');
                        }else{
                            actions.push('<a class="btn btn-primary btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="approval('+row.applyId+','+2+')"><i class="fa fa-check"></i></a> ');
                            actions.push('<a class="btn btn-danger btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="approval('+row.applyId+','+3+')"><i class="fa fa-remove"></i></a> ');
                        }
                       return actions.join('');
                    }
                }]
        };
        $.table.init(options);
    });

    //修改状态
    function approval(applyId,status){
        let str = ''
        if(status == 1){
            str = "取消审批"
        }else if(status == 2){
            str = "通过申请";
            let i = '';
            $.ajax({
                url:prefix + '/ifRent',
                type:'get',
                data:{applyId:applyId},
                dataType:'json',
                async: false,
                success: function(result) {
                    i = result;
                }
            });
            if(i == 2){
                $.modal.alertWarning("房屋已经被出租！");
                return;
            }
        }else if(status == 3){
            str = "拒绝申请"
        }
        $.modal.confirm("确认要"+str+"吗？", function() {
            $.operate.post(prefix + "/editStatus", { "applyId": applyId,status: status});
        })
    }
</script>
</body>
</html>